﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using uAccess;
using WebApi.Models;




namespace WebApi.Services
{
    public class MyGoods
    {
        //新增商品
        public static Boolean CreateGoods( string name, string type, string unit, string label1, string label2, int rank, int state,string activity, decimal? ratio, decimal? dprice, string describe,string picpath,string scrollpath)
        {
            
            var op = new uAccess<Products>("dbConnect");
            var timeStamp = System.DateTime.Now;
            var rv = new Products() {
            //pro_Id=pro_id,
            pro_Name = name,
            pro_Category = type,
            pro_Unit = unit,
            pro_Label = label1,
            pro_2ndLabel = label2,
            pro_Rank = rank,
            pro_State = state,
            pro_Acitivity=activity,
            pro_Ratio = ratio,
            pro_DefaultPrice = dprice,
            pro_Description = describe,
            pro_Photo = picpath,
            pro_TimeStamp=timeStamp,
            pro_Scroll=scrollpath,
            };
            op.Add(rv);
      
            return true;

            }

        //上传滑动图
        public static string upLoadPics(HttpFileCollection pics)
        {
            var picspath = "";
            //检查并创建文件目录
            string fPath = "/images/scroll/";
            //var serverPath = HttpContext.Current.Server.MapPath(fPath + "/");
            if (Directory.Exists(fPath) == false)
                Directory.CreateDirectory(fPath);

            if (pics.Count == 0) return "";

            //循环读取文件，并上传到服务器的指定目录 serverPath
            for (int i = 0; i < pics.Count; i++)
            {
                //获取文件名
                string imgType = Path.GetExtension(pics[i].FileName);
                string fileName = Guid.NewGuid().ToString() + imgType; //使用uuid命名图片



                // 服务器中的文件完整路径
                fPath += fileName;
                picspath += fPath;

                //上传文件到服务器
                pics[i].SaveAs(fPath);
            }
            return picspath;
        }

        //上传图片
        public static string upLoadPics(HttpPostedFile pics)
        {
            var picspath = "";
            //检查并创建文件目录
            string fPath = "/images/product/";
            //var serverPath = HttpContext.Current.Server.MapPath(fPath + "/");
            if (Directory.Exists(fPath) == false)
                Directory.CreateDirectory(fPath);

       
                //获取文件名
                string imgType = Path.GetExtension(pics.FileName);
                string fileName = Guid.NewGuid().ToString() + imgType; //使用uuid命名图片



                // 服务器中的文件完整路径
                fPath += fileName;
                picspath += fPath;

                //上传文件到服务器
                pics.SaveAs(fPath);
            
            return picspath;
        }

        //显示所有商品
        public static JArray GoodsList()
        {
            JArray goods = new JArray();
            var op = new uAccess<Products>("dbConnect").GetAll();
            var goods_str = JsonConvert.SerializeObject(op);
            goods = JArray.Parse(goods_str);
            return goods;
        }

        //显示某一个商品信息
        public static JObject GetGoods(int pro_id)
        {
           // pro_id = pro_id.Substring(1, pro_id.Length - 2);//json传过来时会将"转义为"\，所以要进行字符截取

            uAccess<Products> op = new uAccess<Products>("dbConnect");
            var pr = op.GetFirst(c => c.pro_Id == pro_id);
            JObject goodObj = new JObject
                {
                    {"pro_id",pr.pro_Id},
                    {"pro_name",pr.pro_Name},
                    {"pro_pic",pr.pro_Photo },
                    {"pro_rank",pr.pro_Rank },
                    {"pro_unit",pr.pro_Unit },
                    {"pro_type",pr.pro_Category },
                    {"pro_ratio",pr.pro_Ratio},
                    {"pro_label1",pr.pro_Label },
                    {"pro_label2",pr.pro_2ndLabel },
                    {"pro_dprice",pr.pro_DefaultPrice },
                    {"pro_state",pr.pro_State },
                    {"pro_describe",pr.pro_Description}
                };

            return goodObj;         
        }

        //修改商品信息
        public static Boolean ChangeGoods(int pro_id, string name, string type, string unit, string label1, string label2, int rank, int state, string activity,decimal? ratio, decimal? dprice, string describe, string pic)
        {         
            var op = new uAccess<Products>("dbConnect");
            var pr = op.GetFirst(c => c.pro_Id == pro_id);
            
            pr.pro_Name = name;
            pr.pro_Category = type;        
            pr.pro_Unit = unit;
            pr.pro_Label = label1;
            pr.pro_2ndLabel = label2;
            pr.pro_Rank = rank;
            pr.pro_State = state;
            pr.pro_Acitivity = activity;
            pr.pro_Ratio = ratio;
            pr.pro_DefaultPrice = dprice;
            pr.pro_Description = describe;
            pr.pro_Photo = pic;
            op.Update(pr);
            return true;

        }

        //删除商品
        public static Boolean DeleteGoods(int pro_id)
        {
            //删除该商品对应的所有SKU
            var op = new uAccess<Products>("dbConnect");
            var pr = op.GetFirst(c => c.pro_Id == pro_id);
            var op2 = new uAccess<Inventory>("dbConnect");
            var skus = op2.GetAll(c => c.pro_Id == pro_id);
            foreach(var item in skus)
            {
                op2.Delete(item.SKU_Id);
            }

            //删除该商品
        
            op.Delete(pro_id);
            return true;
        }

        //模糊查询
        public static JArray FuzzySearch(String SearchText)
        {
            JArray customers = new JArray();
            var op = new uAccess<Products>("dbConnect"); //数据库连接对象
            var customers_str = JsonConvert.SerializeObject(op.GetAll(c => c.pro_Name.Contains(SearchText)));
            customers = JArray.Parse(customers_str);
            return customers;
        }


        ////生成pro_Id
        //public static JObject GetPro_Id()
        //{
        //    long timeStamp = DateTimeOffset.Now.ToUnixTimeSeconds();
        //    JObject pro_idObj = new JObject
        //    {
        //        {"pro_id",timeStamp },
        //    };
        //    return pro_idObj;

        //}


    }
}